#include <stdio.h>
#include "wifi_hal.h"
#include "esp_log.h"
#include "string.h"
#include "esp_now_hal.h"
#include "protocol_examples_common.h"
#include "example_common_private.h"

static const char *TAG = "wifi_hal";

static wifi_ap_record_t wifi_hal_panel;
static wifi_hal_t wifi_status_panel;

#if CONFIG_EXAMPLE_WIFI_SCAN_METHOD_FAST
#define EXAMPLE_WIFI_SCAN_METHOD WIFI_FAST_SCAN
#elif CONFIG_EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL
#define EXAMPLE_WIFI_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN
#endif

#if CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL
#define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
#elif CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY
#define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY
#endif

#if CONFIG_EXAMPLE_WIFI_AUTH_OPEN
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN
#elif CONFIG_EXAMPLE_WIFI_AUTH_WEP
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_PSK
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_PSK
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_WPA2_PSK
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_ENTERPRISE
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_ENTERPRISE
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA3_PSK
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_WPA3_PSK
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK
#elif CONFIG_EXAMPLE_WIFI_AUTH_WAPI_PSK
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK
#endif

/**
 * @brief wifi_switch_set
 *
 * @param status
 */
void wifi_switch_set(bool status)
{
    wifi_status_panel.wifi_status = status;
}

/**
 * @brief wifi_switch_panel
 *
 * @return wifi_ap_record_t
 */
wifi_hal_t wifi_switch_panel(void)
{
    return wifi_status_panel;
}

/**
 * @brief wifi_panel
 *
 * @return wifi_ap_record_t
 */
wifi_ap_record_t wifi_panel(void)
{
    return wifi_hal_panel;
}

void set_wifi_hal_conf_proc(bool status)
{
    wifi_status_panel.wifi_connect_proc = status;
}

void set_wifi_hal_conf(uint8_t *ssid, uint8_t *password)
{
    strcpy((char *)wifi_status_panel.ssid, (char *)ssid);
    strcpy((char *)wifi_status_panel.password, (char *)password);
    ESP_LOGI(TAG, "Update wifi conf buff.");
    ESP_LOGI(TAG, "TexTarea-> SSID:%s PASSORD:%s", ssid, password);
    set_wifi_hal_conf_proc(1);
}

/**
 * @brief wifi_hal_connect
 *
 * @return esp_err_t
 */
esp_err_t wifi_hal_connect(void)
{
    app_wifi_init();
    ESP_LOGI(TAG, "Start example_connect.");
    ESP_LOGI(TAG, "SSID:%s PASSORD:%s", wifi_status_panel.ssid, wifi_status_panel.password);
    example_wifi_start();
    wifi_config_t wifi_config = {
        .sta = {
            .scan_method = EXAMPLE_WIFI_SCAN_METHOD,
            .sort_method = EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD,
            .threshold.rssi = CONFIG_EXAMPLE_WIFI_SCAN_RSSI_THRESHOLD,
            .threshold.authmode = EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD,
        },
    };
    strcpy((char *)wifi_config.sta.ssid, (char *)wifi_status_panel.ssid);
    strcpy((char *)wifi_config.sta.password, (char *)wifi_status_panel.password);
    ESP_LOGI(TAG, "WIFI cof successfully.");

    return example_wifi_sta_do_connect(wifi_config, true);
}

/**
 * @brief wifi_status_task
 *
 */
void wifi_status_task(void *arg)
{
    while (1)
    {
        /* get wifi status start */
        esp_wifi_sta_get_ap_info(&wifi_hal_panel);
        /* get wifi status end */
#if WIWI_AHL_DEBUG
        if (wifi_hal_panel.authmode != WIFI_AUTH_OPEN)
        {
            ESP_LOGI(TAG, "WIFI is connect.");
        }
        else
        {
            ESP_LOGI(TAG, "WIFI not connect.");
        }
#endif
        vTaskDelay(pdMS_TO_TICKS(2000));
    }
}